package com.hema.auction.widget.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

import com.hema.auction.fragment.GoodDetailFragment;
import com.hema.auction.fragment.IndextabFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * 作者:john
 */
public class PagerScrollView extends ScrollView {
    private float downY;
    private int mTouchSlop;
    private boolean isScrollViewInBotttom;//是否已经滚动到底部
    private List<Fragment> fragments = new ArrayList<>();
    private RecyclerView recyclerView;
    private int tabIndex;

    private com.hema.auction.listener.OnScrollChangeListener listener;


    public void setListener(com.hema.auction.listener.OnScrollChangeListener listener) {
        this.listener = listener;
    }

    public PagerScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public PagerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public PagerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }

    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    }

    private RecyclerView getRecyclerView() {
        Fragment fragment = fragments.get(tabIndex);
        if (fragment instanceof IndextabFragment) {
            IndextabFragment indextabFragment = (IndextabFragment) fragment;
            return indextabFragment.getRecyclerView();
        } else if (fragment instanceof GoodDetailFragment) {
            GoodDetailFragment detailFragment = (GoodDetailFragment) fragment;
            return detailFragment.getRecyclerView();
        }
        return null;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                if (isScrollViewInBotttom) {
                    if (Math.abs(downY - moveY) > 0) {
                        if (getRecyclerView().canScrollVertically(1) || getRecyclerView().canScrollVertically(-1))
                            return false;
                        return true;
                    }
                } else {
                    if (Math.abs(downY - moveY) >= mTouchSlop)
                        return !isScrollViewInBotttom;
                    return false;
                }

        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (listener != null)
            listener.onScrollChange(this, l, t, oldl, oldt);


        if (getScrollY() == 0) {
            isScrollViewInBotttom = false;
        } else if (getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight()) {
            isScrollViewInBotttom = true;
        } else {
            isScrollViewInBotttom = false;
        }


    }
}
